package cn.zhqr.common.util.number;


import cn.zhqr.common.util.str.StringUtils;

import java.math.BigDecimal;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by charles on 2018/11/16.
 */
public class NumberUtils {
    /**
     * 将int数值按每一位拆分成int数组
     * @param num
     * @return
     */
    public static Integer[] toArray(Integer num){
        if(num==null){
            return new Integer[]{};
        }
        String str = String.valueOf(num);
        Integer[] intArray = new Integer[str.length()];
        for (int i = 0; i < str.length(); i++) {
            Character ch = str.charAt(i);
            intArray[i] = Integer.parseInt(ch.toString());
        }
        return intArray;
    }

    public static boolean isPositive(Integer num){
        return num!=null && num.compareTo(0)>0;
    }
    public static boolean isPositive(Long num){
        return num!=null && num.compareTo(0L)>0;
    }
    public static boolean isPositive(Double num){
        return num!=null && num.compareTo(0.0)>0;
    }
    public static boolean isPositive(Float num){
        return num!=null && num.compareTo(0f)>0;
    }

    public static boolean isPositive(BigDecimal num){
        return num!=null && num.compareTo(BigDecimal.ZERO)>0;
    }

    private static final Pattern PATTERN_NUMBER = Pattern.compile("[0-9]+\\.?[0-9]*");
    private static final Pattern PATTERN_MOBILE = Pattern.compile("^((13[0-9])|(147)|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$");
    public static boolean isNumber(String str){
        if(StringUtils.isBlank(str)){
            return false;
        }
        Matcher isNum = PATTERN_NUMBER.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    public static Boolean isPhoneNumber(String input) {
        return PATTERN_MOBILE.matcher(input).find();
    }

    public static String random(int digit){
        Random r = new Random();
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<digit;i++){
            sb.append(r.nextInt(10));
        }
        return sb.toString();
    }
    public static BigDecimal decimal(Object num){
        if(num==null){
            return null;
        }
        String str = String.valueOf(num);
        if(StringUtils.isBlank(str)){
            return null;
        }
        return new BigDecimal(str);
    }


    /**
     * 计算int数组所有元素之和
     * @param array
     * @return
     */
    public static int arraySum(Integer[] array) {
        int sum = 0;

        for(int a : array){
            sum+=a;
        }
        return sum;
    }

    public static Long parseLong(Object value){
        return parseLong(value,null);
    }

    public static Long parseLong(Object value,Long defaultValue){
        try{
            if(null == value){
                return defaultValue;
            }
            if(value instanceof String && StringUtils.isBlank((String) value)){
                return defaultValue;
            }

            if(value instanceof Number){
                return (Long) value;
            }
            return Long.parseLong(value.toString());
        }catch (Exception e){
            if(defaultValue==null)throw new NumberFormatException("The value "+value+" can't cast to Long");
            return defaultValue;
        }
    }

    public static Integer parseInt(Object value){
        return parseInt(value,null);
    }
    public static Integer parseInt(Object value,Integer defaultValue){
        try{
            if(null == value){
                return defaultValue;
            }
            if(value instanceof String && StringUtils.isBlank((String) value)){
                return defaultValue;
            }

            if(value instanceof Number){
                return (Integer)value;
            }

            return Integer.parseInt(value.toString());
        }catch (Exception e){
            if(defaultValue == null) throw new NumberFormatException("The value "+value+" can't cast to Integer");
            return defaultValue;
        }
    }

    public static Double parseDouble(Object value){
        return parseDouble(value,null);
    }

    public static Double parseDouble(Object value,Double defaultValue){
        try{
            return Double.parseDouble(String.valueOf(value));
        }catch (Exception e){
            if(defaultValue == null){
                throw new NumberFormatException("The value "+value+" can't cast to Double");
            }
            return defaultValue;
        }
    }


    public static BigDecimal null2Zero(BigDecimal num){
        return getNum(num,BigDecimal.ZERO);
    }

    public static Long null2Zero(Long num){
        return getNum(num,0L);
    }

    public static Integer null2Zero(Integer num){
        return getNum(num,0);
    }
    public static <T> T getNum(T obj,T default_value){
        return obj==null?default_value:obj;
    }
}
